package marmalius;

class FletchingTimer implements Runnable {
    Thread t;
    int playerId;
    int item1;
    int amount;
    int xp;
    int finalitem;
	
    FletchingTimer(int playerId, int item1, int finalitem, int amount, int xp) {
        this.playerId = playerId;
        this.item1 = item1;
        this.amount = amount;
        this.xp = xp;
        this.finalitem = finalitem;
        t = new Thread(this);
        t.start();
    }

    public void run() {
        if (finalitem != 52) {
            startfletch();
        }
        if (finalitem == 52) {
            startfletchshafts();
        }
    }
    public int counter = 0;
    public void startfletchshafts() {
        client c = (client) PlayerHandler.getInstance().getPlayer(playerId);

        try {
            Thread.sleep(1700);
        } catch (InterruptedException e) {
            server.Fletching.isfletching = false;
        }
        if (counter < (amount - 1) && server.Fletching.isfletching) {
            c.startAnimation(885);
            c.addItem(finalitem, 20 + misc.random(10));
            c.deleteItem(item1, c.GetItemSlot(item1), 1);
            c.addSkillXP(xp, 9);
            counter++;
            startfletchshafts();
        } else if (counter == (amount - 1) && server.Fletching.isfletching) {
            c.startAnimation(885);
            c.addItem(finalitem, 20 + misc.random(10));
            c.addSkillXP(xp, 9);
            c.deleteItem(item1, c.GetItemSlot(item1), 1);
            c.sendMessage(
                    "You succesfully fletched " + amount * 30 + " "
                    + c.GetItemName(finalitem) + "'s.");
            server.Fletching.isfletching = false;
            counter = 0;
        } else if (!server.Fletching.isfletching) {
            c.sendMessage(
                    "You fletched " + counter * 30 + " "
                    + c.GetItemName(finalitem) + "'s instead of the planned "
                    + amount * 30 + " " + c.GetItemName(finalitem) + ".");
        } else {
            c.sendMessage(
                    "an error occured while fletching, please try again later");
            server.Fletching.isfletching = false;
        }
    }

    public void startfletch() {
        client c = (client) PlayerHandler.getInstance().getPlayer(playerId);

        try {
            Thread.sleep(2400);
        } catch (InterruptedException e) {
            server.Fletching.isfletching = false;
        }
        if (counter < (amount - 1) && server.Fletching.isfletching) {
            c.startAnimation(1248);
            c.addItem(finalitem, 1);
            c.deleteItem(item1, c.GetItemSlot(item1), 1);
            c.addSkillXP(xp, 9);
            counter++;
            startfletch();
        } else if (counter == (amount - 1) && server.Fletching.isfletching) {
            c.startAnimation(1248);
            c.addItem(finalitem, 1);
            c.addSkillXP(xp, 9);
            c.deleteItem(item1, c.GetItemSlot(item1), 1);
            c.sendMessage(
                    "You succesfully fletched " + amount + " "
                    + c.GetItemName(finalitem) + "'s.");
            server.Fletching.isfletching = false;
            counter = 0;
        } else if (!server.Fletching.isfletching) {
            c.sendMessage(
                    "You fletched " + counter + " " + c.GetItemName(finalitem)
                    + "'s instead of the planned " + amount + " "
                    + c.GetItemName(finalitem) + ".");
        } else {
            c.sendMessage(
                    "an error occured while fletching, please try again later");
            server.Fletching.isfletching = false;
        }
    }

}
